// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.util;

import java.util.function.BiFunction;

public class StringUtils {
    public static final String s_emptyString = "";
    public static final char UTF8_BOM = '\ufeff';

    public static boolean isNullOrEmpty(String s) {
        return s == null || s.length() == 0;
    }

    public static boolean isNullOrWhitespace(String s) {
        return isNullOrEmpty(s) || isWhitespace(s);
    }

    private static boolean isWhitespace(String string) {
        int int0 = string.length();
        if (int0 <= 0) {
            return false;
        } else {
            int int1 = 0;
            int int2 = int0 / 2;

            for (int int3 = int0 - 1; int1 <= int2; int3--) {
                if (!Character.isWhitespace(string.charAt(int1)) || !Character.isWhitespace(string.charAt(int3))) {
                    return false;
                }

                int1++;
            }

            return true;
        }
    }

    public static String discardNullOrWhitespace(String str) {
        return isNullOrWhitespace(str) ? null : str;
    }

    public static String trimPrefix(String str, String prefix) {
        return str.startsWith(prefix) ? str.substring(prefix.length()) : str;
    }

    public static String trimSuffix(String str, String suffix) {
        return str.endsWith(suffix) ? str.substring(0, str.length() - suffix.length()) : str;
    }

    public static boolean equals(String a, String b) {
        return a == b ? true : a != null && a.equals(b);
    }

    public static boolean startsWithIgnoreCase(String str, String prefix) {
        return str.regionMatches(true, 0, prefix, 0, prefix.length());
    }

    public static boolean endsWithIgnoreCase(String str, String suffix) {
        int _int = suffix.length();
        return str.regionMatches(true, str.length() - _int, suffix, 0, _int);
    }

    public static boolean containsIgnoreCase(String haystack, String needle) {
        for (int _int = haystack.length() - needle.length(); _int >= 0; _int--) {
            if (haystack.regionMatches(true, _int, needle, 0, needle.length())) {
                return true;
            }
        }

        return false;
    }

    public static boolean equalsIgnoreCase(String a, String b) {
        return a == b ? true : a != null && a.equalsIgnoreCase(b);
    }

    public static boolean tryParseBoolean(String varStr) {
        if (isNullOrWhitespace(varStr)) {
            return false;
        } else {
            String string = varStr.trim();
            return string.equalsIgnoreCase("true") || string.equals("1") || string.equals("1.0");
        }
    }

    public static boolean isBoolean(String varStr) {
        String string = varStr.trim();
        return string.equalsIgnoreCase("true") || string.equals("1") || string.equals("1.0")
            ? true
            : string.equalsIgnoreCase("false") || string.equals("0") || string.equals("0.0");
    }

    public static boolean contains(String[] string0, String string1, BiFunction<String, String, Boolean> biFunction) {
        return indexOf(string0, string1, biFunction) > -1;
    }

    public static int indexOf(String[] string0, String string1, BiFunction<String, String, Boolean> biFunction) {
        int int0 = -1;

        for (int int1 = 0; int1 < string0.length; int1++) {
            if ((Boolean)biFunction.apply(string0[int1], string1)) {
                int0 = int1;
                break;
            }
        }

        return int0;
    }

    public static String indent(String text) {
        return indent(text, "", "\t");
    }

    private static String indent(String string1, String string2, String string3) {
        String string0 = System.lineSeparator();
        return indent(string1, string0, string2, string3);
    }

    private static String indent(String string0, String string1, String string2, String string3) {
        if (isNullOrEmpty(string0)) {
            return string0;
        } else {
            int int0 = string0.length();
            StringBuilder stringBuilder0 = new StringBuilder(int0);
            StringBuilder stringBuilder1 = new StringBuilder(int0);
            int int1 = 0;

            for (int int2 = 0; int2 < int0; int2++) {
                char _char = string0.charAt(int2);
                switch (_char) {
                    case '\n':
                        stringBuilder0.append(stringBuilder1);
                        stringBuilder0.append(string1);
                        stringBuilder1.setLength(0);
                        int1++;
                    case '\r':
                        break;
                    default:
                        if (stringBuilder1.length() == 0) {
                            if (int1 == 0) {
                                stringBuilder1.append(string2);
                            } else {
                                stringBuilder1.append(string3);
                            }
                        }

                        stringBuilder1.append(_char);
                }
            }

            stringBuilder0.append(stringBuilder1);
            stringBuilder1.setLength(0);
            return stringBuilder0.toString();
        }
    }

    public static String leftJustify(String text, int length) {
        if (text == null) {
            return leftJustify("", length);
        } else {
            int int0 = text.length();
            if (int0 >= length) {
                return text;
            } else {
                int int1 = length - int0;
                char[] _char = new char[int1];

                for (int int2 = 0; int2 < int1; int2++) {
                    _char[int2] = ' ';
                }

                String string = new String(_char);
                return text + string;
            }
        }
    }

    public static String moduleDotType(String module, String type) {
        if (type == null) {
            return null;
        } else {
            return type.contains(".") ? type : module + "." + type;
        }
    }

    public static String stripBOM(String line) {
        return line != null && line.length() > 0 && line.charAt(0) == '\ufeff' ? line.substring(1) : line;
    }

    public static boolean containsDoubleDot(String string) {
        return isNullOrEmpty(string) ? false : string.contains("..") || string.contains("\u0000.\u0000.");
    }
}
